package com.example.gomi.intelligentcitypipe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gomi.intelligentcitypipe.DAO.PipeInfo;
import com.example.gomi.intelligentcitypipe.DAO.PipeInfoHelper;

import java.sql.SQLException;

public class PipeDetailActivity extends AppCompatActivity {

    private TextView mIDTextView;
    private TextView mPipeIDTextView;
    private TextView mPipeTypeTextView;
    private TextView mPipeDateTextView;
    private TextView mPipeCompanyTextView;
    private TextView mPipeJobIDTextView;
    private TextView mPipeLatitudeTextView;
    private TextView mPipeLongitudeTextView;

    /*温度*/
    private TextView temperature;
    /*湿度*/
    private TextView humidity;
    private Button mDetailDeleteButton;

    private PipeInfo mPipeInfo = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pipe_detail);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        mIDTextView = (TextView) findViewById(R.id.detailID);
        mPipeIDTextView = (TextView) findViewById(R.id.detailPipeID);
        mPipeTypeTextView = (TextView) findViewById(R.id.detailType);
        mPipeDateTextView = (TextView) findViewById(R.id.detailDate);
        mPipeCompanyTextView = (TextView) findViewById(R.id.detailCompany);
        mPipeJobIDTextView = (TextView) findViewById(R.id.detailJobID);
        mPipeLatitudeTextView = (TextView) findViewById(R.id.detailLatitude);
        mPipeLongitudeTextView = (TextView) findViewById(R.id.detailLongitude);
        temperature = (TextView) findViewById(R.id.temperature);
        humidity = (TextView) findViewById(R.id.humidity);
        mDetailDeleteButton = (Button) findViewById(R.id.detailDelete);
        mDetailDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detailDeleteOnClick(view);
            }
        });

        PipeInfoHelper.setmContext(this);

        Intent intent = this.getIntent();
        int id = intent.getIntExtra("id", -1);
        if (id == -1) {
            finish();
        }

        try {
            mPipeInfo = PipeInfoHelper.getInstance().queryById(id);
            mIDTextView.setText("序号：" + Integer.toString(mPipeInfo.getId()));
            mPipeIDTextView.setText("EPC编码：" + Long.toString(mPipeInfo.getPipeid()));
//            mPipeTypeTextView.setText(Integer.toString(mPipeInfo.getType()));
            mPipeTypeTextView.setText("管道类型：" + PipeInfo.convertType(mPipeInfo.getType()));
            String tmp = Integer.toString(mPipeInfo.getYear()) + "-" + Integer.toString(mPipeInfo.getMonth()) + "-" + Integer.toString(mPipeInfo.getDay()) + "  "
                    + Integer.toString(mPipeInfo.getHour()) + ":" + Integer.toString(mPipeInfo.getMinute()) + "\n";
            mPipeDateTextView.setText("填埋时间：" + tmp);
//            mPipeCompanyTextView.setText(Integer.toString(mPipeInfo.getCompany()));
//            mPipeJobIDTextView.setText(Integer.toString(mPipeInfo.getJobid()));
            mPipeCompanyTextView.setText("公司名：" + PipeInfo.convertCompany(mPipeInfo.getCompany()));
            mPipeJobIDTextView.setText("责任人：" + PipeInfo.convertJobID(mPipeInfo.getJobid()));
            mPipeLatitudeTextView.setText("纬度：" + mPipeInfo.getLatitude());
            mPipeLongitudeTextView.setText("经度：" + mPipeInfo.getLongitude());
            temperature.setText("温度：" +String.format("%.2f°C",  mPipeInfo.getTemperature()));
            humidity.setText("湿度：" + String.format("%.2f%%",  mPipeInfo.getHumidity()));
        } catch (SQLException e) {
            e.printStackTrace();
            Toast.makeText(this, "Parameter error", Toast.LENGTH_SHORT).show();
            this.finish();
        } catch (NullPointerException e) {
            e.printStackTrace();
            Toast.makeText(this, "Parameter error", Toast.LENGTH_SHORT).show();
            this.finish();
        }

    }

    private void detailDeleteOnClick(View view) {
        if (mPipeInfo != null) {
            try {
                PipeInfoHelper.getInstance().delete(mPipeInfo);
                setResult(RESULT_OK);
                finish();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        PipeInfoHelper.setmContext(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        PipeInfoHelper.closeHelper();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PipeInfoHelper.closeHelper();
    }

}
